package com.net.xpay.stable.job;

import com.net.common.concurrent.ThreadUtil;
import com.net.xpay.common.helper.CommonSysConfigHelper;
import com.net.xpay.common.helper.NoticeHelper;
import com.net.xpay.core.enums.SafePayBatchStatus;
import com.net.xpay.core.manager.safePay.SafePayBatchManager;
import com.net.xpay.core.model.SafePayBatchSearchModel;
import com.net.xpay.core.service.safePay.SafePayService;
import com.net.shard.ShardUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Slf4j
public class SafePayBatchBankTransUpdateJob implements Runnable {

    private long shardId;

    private SafePayBatchManager safePayBatchManager;

    private SafePayService safePayService;

    private CommonSysConfigHelper commonSysConfigHelper;

    private NoticeHelper noticeHelper;

    public SafePayBatchBankTransUpdateJob(Long shardId, SafePayBatchManager safePayBatchManager,
                                          SafePayService safePayService, CommonSysConfigHelper commonSysConfigHelper, NoticeHelper noticeHelper) {
        this.shardId = shardId;
        this.safePayBatchManager = safePayBatchManager;
        this.safePayService = safePayService;
        this.commonSysConfigHelper = commonSysConfigHelper;
        this.noticeHelper = noticeHelper;
    }

    @Override
    public void run() {
        try {
            ShardUtil.setShardContext(shardId);
            while (true) {
                try {
                    doUpdateSafePayBatchBankTrans();
                } catch (Throwable e) {
                    log.error("doUpdateSafePayBatchBankTrans", e);
                }
                ThreadUtil.safeSleep(500, "");
            }
        } finally {
            ShardUtil.cleanShardContext();
        }
    }

    private void doUpdateSafePayBatchBankTrans() {
        SafePayBatchSearchModel searchModel = new SafePayBatchSearchModel();
        searchModel.setStatus(SafePayBatchStatus.DEALING);//处理中的

        List<Long> batchIds = safePayBatchManager.listIds(searchModel);
        for (Long batchId : batchIds) {
            try {
                safePayService.updateDealingBankBatch(batchId);
            } catch (Exception e) {
                log.error("doUpdateSafePayBatchBankTrans error|batchId={}", batchId, e);
            }
        }
    }


}
